#include <bits/stdc++.h>
using namespace std;
const int N = 15;

int a[N][5], cnt[N];

void dfs(int x)
{
	if(x == 13) return;
	int t = a[x][cnt[x]];
	cnt[x] --;
	dfs(t);
}

int main()
{
	for(int i = 1;i <= 13;i ++)
	{
		cnt[i] = 4;
		for(int j = 1;j <= 4;j ++)
		{
			char c; cin >> c;
			if(c == '0') a[i][j] = 10;
			else if(c == 'J') a[i][j] = 11;
			else if(c == 'Q') a[i][j] = 12;
			else if(c == 'K') a[i][j] = 13;
			else if(c == 'A') a[i][j] = 1;
			else a[i][j] = c - '0';
		}
	}
	for(int i = 1;i <= 4;i ++) dfs(a[13][i]);
	int ans = 0;
	for(int i = 1;i <= 13;i ++)	if(!cnt[i]) ans ++;
	cout << ans << endl;
	return 0;
}